package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IZuulFilterServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ZuulFilterAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ZuulFilterPageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ZuulFilterUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.ZuulFilterAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.ZuulFilterPageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.ZuulFilter;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.ZuulFilterEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.ZuulFilterQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.ZuulFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;

@RestController
public class ZuulFilterServiceRemoteImpl implements IZuulFilterServiceRemote {
    @Autowired
    private ZuulFilterService zuulFilterService;

    @Override
    public ResponseData<ZuulFilterPageAResp> list(ZuulFilterPageAReq zuulFilterPageAReq) {
        ResponseData<ZuulFilterPageAResp> responseData = new ResponseData<>();

        ZuulFilterQuery zuulFilterQuery = new ZuulFilterQuery();
        if (Detect.notEmpty(zuulFilterPageAReq.getLikeComment())) {
            zuulFilterQuery.setLikeComment(URLDecoder.decode(zuulFilterPageAReq.getLikeComment()));
        }
        if (zuulFilterPageAReq.getStatus() != null) {
            zuulFilterQuery.setStatuses(Lists.newArrayList(zuulFilterPageAReq.getStatus()));
        }

        if (zuulFilterPageAReq.getProgress() != null) {
            zuulFilterQuery.setProgresses(Lists.newArrayList(zuulFilterPageAReq.getProgress()));
        }
        if (Detect.notEmpty(zuulFilterPageAReq.getFilterType())) {
            zuulFilterQuery.setFilterTypes(Lists.newArrayList(zuulFilterPageAReq.getFilterType()));
        }
        zuulFilterQuery.setApplicationNames(Lists.newArrayList(zuulFilterPageAReq.getGateway()));

        Pagination<ZuulFilterEnhanced> pagination = new Pagination(1, Integer.MAX_VALUE, false);
        pagination.addOrder(new Order(ZuulFilter.COLUMN_STATUS, Order.ORDER_DESC), ZuulFilter.class)
                .addOrder(new Order(ZuulFilter.COLUMN_PROGRESS, Order.ORDER_DESC), ZuulFilter.class)
                .addOrder(new Order(ZuulFilter.COLUMN_FILTER_TYPE, Order.ORDER_ASC), ZuulFilter.class)
                .addOrder(new Order(ZuulFilter.COLUMN_FILTER_ORDER, Order.ORDER_ASC), ZuulFilter.class)
                .addOrder(new Order(ZuulFilter.COLUMN_CREATE_TIME, Order.ORDER_ASC), ZuulFilter.class);

        pagination = zuulFilterService.getPagination(zuulFilterQuery, pagination, null);

        ZuulFilterPageAResp zuulFilterPageAResp = new ZuulFilterPageAResp();
        zuulFilterPageAResp.setCount(pagination.getCount());
        zuulFilterPageAResp.setPage(1);
        zuulFilterPageAResp.setSize(Integer.MAX_VALUE);

        if (Detect.notEmpty(pagination.getItems())) {
            zuulFilterPageAResp.setItems(CommonConvent.convent2ZuulFilterAResps(pagination.getItems()));
        }

        responseData.setData(zuulFilterPageAResp);

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<ZuulFilterUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        ZuulFilterUpdateAReq zuulFilterUpdateAReq = requestData.getRequest();

        zuulFilterService.updateZuulFilter(zuulFilterUpdateAReq.getId(), zuulFilterUpdateAReq.getStatus(), zuulFilterUpdateAReq.getProgress(), zuulFilterUpdateAReq.getFilterCode(), zuulFilterUpdateAReq.getComment(), null);

        return responseData;
    }

    @Override
    public ResponseData<ZuulFilterAResp> add(@RequestBody RequestData<ZuulFilterAddAReq> requestData) {
        ResponseData<ZuulFilterAResp> responseData = new ResponseData<>();

        ZuulFilterAddAReq zuulFilterAddAReq = requestData.getRequest();

        ZuulFilter zuulFilter = zuulFilterService.addZuulFilter(zuulFilterAddAReq.getFilterCode(), zuulFilterAddAReq.getComment(), null);

        responseData.setData(CommonConvent.convent2ZuulFilterAResp((ZuulFilterEnhanced) zuulFilterService.convent2Enhanced(zuulFilter)));

        return responseData;
    }

    @Override
    public ResponseData<ZuulFilterAResp> detail(@PathVariable("id") Integer id) {
        ResponseData<ZuulFilterAResp> responseData = new ResponseData<>();

        responseData.setData(CommonConvent.convent2ZuulFilterAResp((ZuulFilterEnhanced) zuulFilterService.getEnhanced(id, null)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") Integer id) {
        ResponseData responseData = new ResponseData();

        zuulFilterService.deleteZuulFilter(id, null);

        return responseData;
    }

}
